<template>
    <div class="adminRole">
        <el-form ref="formRef" :inline="true" :model="form" class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]">
            <el-form-item :label="t('station.stationName') + ':'" prop="stationname">
                <el-input v-model="form.stationname" :placeholder="t('station.enterStationName')" clearable
                    class="!w-[230px]" />
            </el-form-item>
            <el-form-item :label="t('station.xAxis') + ':'" prop="x">
                <el-input v-model="form.x" clearable class="!w-[230px]" :placeholder="t('station.inputXAxis')" />
            </el-form-item>
            <el-form-item :label="t('station.yAxis') + ':'" prop="y">
                <el-input v-model="form.y" clearable class="!w-[230px]" :placeholder="t('station.inputYAxis')" />
            </el-form-item>
            <el-form-item :label="t('station.siteType') + ':'" prop="stationtype">
                <el-select v-model="form.stationtype" class="!w-[230px]">
                    <el-option v-for="item in stationTypes" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('station.status') + ':'" prop="status">
                <el-select v-model="form.status" clearable class="!w-[230px]">
                    <el-option :label="t('station.all')" :value="''" />
                    <el-option :label="t('station.enable')" :value="1" />
                    <el-option :label="t('station.disable')" :value="0" />
                    <el-option :label="t('station.lock')" :value="2" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch">
                    {{ t("station.search") }}
                </el-button>
                <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
                    {{ t("station.reset") }}
                </el-button>
            </el-form-item>
        </el-form>

        <PureTableBar :title="t('station.stationManagement')" :columns="columns" @refresh="onSearch" v-if="tableHtml">
            <template #buttons>
                <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="openDialog()">
                    {{ t("station.addNewSite") }}
                </el-button>
            </template>
            <template v-slot="{ size, dynamicColumns }">
                <pure-table ref="tableRef" adaptive row-key="Id" showOverflowTooltip table-layout="auto"
                    :loading="loading" :size="size" :data="dataList" :columns="dynamicColumns" :pagination="pagination"
                    :paginationSmall="size === 'small' ? true : false" :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                    }" @page-size-change="handleSizeChange" @page-current-change="handleCurrentChange">
                    <template #operation="{ row }" align-whole="right">
                        <el-button class="reset-margin" link type="success" :size="size"
                            v-if="row.status === 0 || row.status === 2" :icon="useRenderIcon(Check)"
                            @click="stationEnable(row)">
                            {{ t("station.enable") }}
                        </el-button>
                        <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
                            @click="stationUpdate(row?.id)">
                            {{ t("station.edit") }}
                        </el-button>
                        <el-button class="reset-margin" link type="danger" :size="size"
                            :icon="useRenderIcon(outlineDelete)" @click="handleDelete(row)">
                            {{ t("station.delete") }}
                        </el-button>
                    </template>
                </pure-table>
            </template>
        </PureTableBar>
    </div>
</template>

<script lang='ts' setup>
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useDept } from "./utils/hook";
import { ref, watch, nextTick } from 'vue'
import { getStation, updateStation } from '@/utils/api/dispatch/station'
import { message } from "@/utils/message";
import { ElMessageBox } from 'element-plus'
import { useTranslationLang } from "@/layout/hooks/useTranslationLang";
const { locale } = useTranslationLang();
import { useI18n } from "vue-i18n";
const { t } = useI18n();

// iconify-icons
import AddFill from "@iconify-icons/ri/add-circle-line";
import EditPen from "@iconify-icons/ep/edit-pen";
import outlineDelete from '@iconify-icons/ic/outline-delete';
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import Check from "@iconify-icons/ep/Check";

const formRef = ref();

const tableHtml = ref(true);

watch(() => locale.value, (val) => {
    tableHtml.value = false
    nextTick(() => {
        tableReloading()
        tableHtml.value = true
    })
})

// 站点启用
function stationEnable(data) {
    ElMessageBox.confirm(
        t("station.promptText"),
        t("station.prompt"),
        {
            confirmButtonText: t("station.confirm"),
            cancelButtonText: t("station.cancel"),
            type: 'warning',
            center: true,
        }
    ).then(() => {
        data.status = 1
        updateStation(data).then(res => {
            if (res.Code === 0) {
                message(t("station.enabledSuccessfully"), {
                    type: "success"
                });
                onSearch()
            }
        })
    }).catch(() => { })

}

// 站点编辑
async function stationUpdate(id: number) {
    let { Content } = await getStation(id)
    //@ts-ignore
    openDialog('编辑', Content)
}

const {
    columns,
    loading,
    dataList,
    openDialog,
    handleDelete,
    form,
    onSearch,
    resetForm,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    stationTypes,
    tableReloading
} = useDept();

</script>

<style lang="scss" scoped>
.search-form {
    :deep(.el-form-item) {
        margin-bottom: 12px;
    }
}
</style>